jQ(function(){
	jQ("#sublistcontent").data("currentpage",0);
	jQ("#unsublistcontent").data("currentpage",0);
	loadUserSubscribed();
	loadUserUnsubscribed();
});

function checkSubSearchFocus(obj){
	if(jQ(obj).hasClass("default")&&jQ(obj).val()=="订阅搜索"){
		jQ(obj).val("").removeClass().addClass("normal");
	}
	
}

function checkSubSearchBlur(obj){
	if(jQ(obj).hasClass("normal")&&jQ(obj).val().length==0){
		jQ(obj).val("订阅搜索").removeClass().addClass("default");
	}
	
}

function loadUserSubscribed(){
	var currentpage=eval(jQ("#sublistcontent").data("currentpage"));
	jQ("#sublisttitle").nextAll().remove();
	jQ("#sublistcontent").empty();
	jQ.ajax({
		type: "POST",
		url: "userAjax!loadUserSubscribed.action",
		data:{
			currentpage:currentpage
		},
		success:function(data){
			var html="";
			jQ("User",data).each(function(i){
				var uid=jQ(this).attr("id");
				var name=jQ(this).find("name").text();
				var smalllogo=jQ(this).find("smalllogo").text();
				var subscribedcount=eval(jQ(this).find("subscribedcount").text());
				var linedouble=i%10;
				var line=i%5;
				if(line==0){
					if(linedouble==0){
						html+="<tr >";
					}else{
						html+="<tr class='double'>";
					}
				}
				html+="<td uid="+uid+">";
				html+="<div class=\"userlogo\" style=\"background:url('"+smalllogo+"')\"></div>";
				html+="<div class=\"content\">";
				html+="<a class=\"name\" href='"+jQ("#ctxVal").val()+"/myclip/user.html?"+uid+"'>"+name+"</a>";
				html+="<a class=\"opt\" onclick='unsubscribeCurrent(this);'>取消订阅</a>";
				html+="<span class='count'>"+subscribedcount+"</span>";
				html+="<span class='icon'></span>";
				html+="</div>";
				html+="</td>";
				if((i+1)%5==0){
					html+="</tr>";
				}
			});
			if(jQ("User",data).length>0){
				jQ("#sublistcontent").append(html);
				var currentpage=eval(jQ("currentpage",data).text());
				var pagecount=eval(jQ("pagecount",data).text());
				initPaging(currentpage,pagecount,jQ("#sublisttitle"),"sublistcontent",1);
			}else{
				html+="<tr><td><div class=\"userlogo\"></div><div class=\"content\"><a class=\"name\">暂无订阅</a></div></td><td></td><td></td><td></td><td></td></tr>";
				jQ("#sublistcontent").append(html);
			}
		}
	});
}

function loadUserUnsubscribed(){
	var currentpage=eval(jQ("#unsublistcontent").data("currentpage"));
	jQ("#unsublisttitle").nextAll().remove();
	jQ("#unsublistcontent").empty();
	jQ.ajax({
		type: "POST",
		url: "userAjax!loadAllsubscribed.action",
		data:{
			currentpage:currentpage
		},
		success:function(data){
			var html="";
			jQ("User",data).each(function(i){
				var uid=jQ(this).attr("id");
				var name=jQ(this).find("name").text();
				var biglogo=jQ(this).find("biglogo").text();
				var desc=jQ(this).find("desc").text();
				var issubscribed=eval(jQ(this).find("issubscribed").text());
				var subscribedcount=eval(jQ(this).find("subscribedcount").text());
				
				var linedouble=i%4;
				var line=i%2;
				if(line==0){
					if(linedouble==0){
						html+="<tr >";
					}else{
						html+="<tr class='double'>";
					}
				}
				html+="<td uid="+uid+" id='ss-"+uid+"'>";
				html+="<div class=\"userlogo\" style=\"background:url('"+biglogo+"')\"></div>";
				html+="<div class=\"content\">";
				html+="<div class=\"name\">";
				html+="<a class=\"name\" href='"+jQ("#ctxVal").val()+"/myclip/user.html?"+uid+"'>"+name+"</a>";
				if(issubscribed)
					html+="<a class=\"unsubscribe\"></a>";
				else
					html+="<a class=\"subscribe\"></a>";
				html+="<div title='订阅人数'><span class='icon'></span><span class='count'>"+subscribedcount+"</span></div>";
				html+="</div>";
				html+="<span class=\"desc\">";
				html+=desc;
				html+="</span>";
				html+="</div>";
				html+="</td>";
				if((i+1)%2==0){
					html+="</tr>";
				}
			});
			if(jQ("User",data).length>0){
				jQ("#unsublistcontent").append(html);
				jQ("#unsublistcontent a.unsubscribe").unbind("click").bind("click",unsubscribe);
				jQ("#unsublistcontent a.subscribe").unbind("click").bind("click",subscribe);
				var currentpage=eval(jQ("currentpage",data).text());
				var pagecount=eval(jQ("pagecount",data).text());
				initPaging(currentpage,pagecount,jQ("#unsublisttitle"),"unsublistcontent",0);
			}else{
				html+="<tr><td><div class=\"userlogo\"></div><div class=\"content\"><div class=\"name\"><a class=\"name\">暂无订阅</a></div></div></td><td></td></tr>";
				jQ("#unsublistcontent").append(html);
			}
		}
	});
}

function initPaging(currentpage,pagecount,pagetitleobj,setobjid,issubscribe){
	if(pagecount==1){
		return;
	}else{
		var pagemin=currentpage-2;
		var pagemax=currentpage+2;
		if(pagemin<0){
			pagemin=0;
		}
		if(pagemax>pagecount-1){
			pagemax=pagecount-1;
		}
		var html="<div class=\"pagecount\" >共有"+pagecount+"页</div>";
		html+="<div class=\"page\">";
		html+="<a page='first'><span >&lt;&lt;</span></a>";
		for(var i=pagemin;i<=pagemax;i++){
			if(i==currentpage){
				html+="<b ><span>"+(i+1)+"</span></b>";
			}else
				html+="<a page='"+i+"'><span>"+(i+1)+"</span></a>";
		}
		html+="<a page='last'><span >&gt;&gt;</span></a>";
		html+="</div>";
		jQ(pagetitleobj).after(html);
		jQ(pagetitleobj).nextAll("div.page").find("a").unbind("click").bind("click",function(){
			var page=jQ(this).attr("page");
			var currentpage=0;
			if(page=="first"){
				currentpage=0;
			}else if(page=="last"){
				currentpage=pagecount-1;
			}else{
				currentpage=eval(page);
			}
			jQ("#"+setobjid).data("currentpage",currentpage);
			if(issubscribe){
				loadUserSubscribed();
			}else{
				loadUserUnsubscribed();
			}
		});
	}
}

function subscribe(){
	var thisobj=jQ(this);
	var nameobj=jQ(this).parent();
	var contentobj=nameobj.parent();
	var tdobj=contentobj.parent();
	var uid=eval(tdobj.attr("uid"));
	jQ.ajax({
		type:"POST",
		url: "contactAjax!subscribeCC.action",
		data:{
			uid:uid
		},
		success:function(data){
			var dataObj=eval("("+data+")");//转换为json对象
			if(dataObj.result){
				loadUserSubscribed();
				thisobj.removeClass().addClass("unsubscribe");
				thisobj.unbind("click").bind("click",unsubscribe);
			}
		}
	});
}
function unsubscribe(){
	var thisobj=jQ(this);
	var nameobj=jQ(this).parent();
	var name=nameobj.find("a.name").text();
	var contentobj=nameobj.parent();
	var tdobj=contentobj.parent();
	var uid=eval(tdobj.attr("uid"));
	invitingOptReturnBox(jQ(this),"确定不再订阅 <font>"+name+"</font> ?");
	jQ("#msg_box_opt a.confirm").unbind("click").bind("click",function(){
		jQ.ajax({
			type:"POST",
			url: "contactAjax!unsubscribeCC.action",
			data:{
				uid:uid
			},
			success:function(data){
				var dataObj=eval("("+data+")");//转换为json对象
				if(dataObj.result){
					jQ("#msg_box").find("div.msg_note").animate({top: 200+'px'},500,function(){
						jQ("#msg_box").hide();
						loadUserSubscribed();
						thisobj.removeClass().addClass("subscribe");
						thisobj.unbind("click").bind("click",subscribe);
					});
					
				}
			}
		});
	});
	jQ("#msg_box_opt a.cancel").unbind("click").bind("click",function(){
		jQ("#msg_box").find("div.msg_note").animate({top: 200+'px'},500,function(){jQ("#msg_box").hide();});
	});
}


function unsubscribeCurrent(obj){
	var contentobj=jQ(obj).parent();
	var name=jQ(obj).prev().text();
	var tdobj=contentobj.parent();
	var uid=eval(tdobj.attr("uid"));
	invitingOptReturnBox(obj,"确定不再订阅 <font>"+name+"</font> ?");
	jQ("#msg_box_opt a.confirm").unbind("click").bind("click",function(){
		jQ.ajax({
			type:"POST",
			url: "contactAjax!unsubscribeCC.action",
			data:{
				uid:uid
			},
			success:function(data){
				var dataObj=eval("("+data+")");//转换为json对象
				if(dataObj.result){
					jQ("#msg_box").find("div.msg_note").animate({top: 200+'px'},500,function(){
						jQ("#msg_box").hide();
						loadUserSubscribed();
						jQ("#ss-"+uid+" div.content div.name a.unsubscribe").removeClass("unsubscribe").addClass("subscribe")
							.unbind("click").bind("click",subscribe);
						
					});
					
				}
			}
		});
	});
	jQ("#msg_box_opt a.cancel").unbind("click").bind("click",function(){
		jQ("#msg_box").find("div.msg_note").animate({top: 200+'px'},500,function(){jQ("#msg_box").hide();});
	});
	
}

function invitingOptReturnBox(obj,content){
	var obj_box = jQ(obj);
	var obj_offset = obj_box.offset();
	var msg_box = jQ("#msg_box");
	jQ("#msg_box_content").empty().append(content);
	var _top = obj_offset.top - msg_box.outerHeight() +10;
	var _left = obj_offset.left - msg_box.outerHeight()/2;
	msg_box.css({"top":_top+"px","left":_left+"px"}).show();
	msg_box.find("div.msg_note").animate({top: 20+'px'}, 500,function(){
		//setTimeout(function(){
		//	
		//},2000);
	});
}

function searchKeyDown(event){
	if(event.keyCode==13){
		searchSubscribe();
	}
}

function searchSubscribe(){
	var inputobj=jQ("#searchcontent");
	var val=jQ.trim(inputobj.val());
	if(inputobj.hasClass("default"))
		val="";
	var tm= encodeURI(val);
	tm=encodeURI(tm);
	if(val.length==0){
		window.location.href=jQ("#ctxVal").val()+"/contact/search.html?usertype=2";
	}else{
		window.location.href=jQ("#ctxVal").val()+"/contact/search.html?usertype=2&content="+tm;
	}
	
}
